package aryn.framework.display;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseListenerAdapter extends MouseAdapter {
	
	private int width;
	private int height;
	private MyMouseInfo info;
	
	public MouseListenerAdapter(MyCanvas canvas) {
		this.width = canvas.getWidth();
		this.height = canvas.getHeight();
		this.info = canvas.getMouseInfo();
	}
	
	public void mouseClicked(MouseEvent e) {
		setMouse(e);
		info.setClicked(true);
	}
	
	public void mouseEntered(MouseEvent e) {
		setMouse(e);
		info.setEntered(true);
	}
	
	public void mouseExited(MouseEvent e) {
		setMouse(e);
		info.setExited(true);
	}
	
	public void mousePressed(MouseEvent e) {	
		setMouse(e);
		info.setPressed(true);
	}
	
	public void mouseReleased(MouseEvent e) {
		setMouse(e);
		info.setReleased(true);
	}
	
	private void setMouse(MouseEvent e) {
		info.reset();
		info.setClick(e.getButton());
		info.setClickCount(e.getClickCount());
		int mx = e.getX();
		int my = e.getY();
		int gmx = e.getXOnScreen();
		int gmy = e.getYOnScreen();
		if(mx < 0) mx = 0;		if(mx > width) mx = width;
		if(my < 0) my = 0;		if(my > height) my = height;
		if(gmx < 0) gmx = 0;	if(gmx > width) gmx = width;
		if(gmy < 0) gmy = 0;	if(gmy > height) gmy = height;
		info.setMouseX(mx);
		info.setMouseY(my);
		info.setGlobalMouseX(gmx);
		info.setGlobalMouseY(gmy);
	}
}
